/*
 * TestPlaneVisometry.java
 *
 * Created on Jul 30, 2009, 3:15:03 PM
 */
package org.bm.blaise.specto.plane.graph;

import java.awt.Color;
import org.bm.firestarter.propertysheet.PropertySheet;
import org.bm.blaise.style.BasicPointStyle;
import org.bm.blaise.style.PointStyle;
import org.bm.blaise.scio.graph.GraphFactory;
import org.bm.blaise.scio.graph.PrimeNumberGraph;
import org.bm.blaise.scio.graph.layout.SpringLayout;
import org.bm.blaise.scio.graph.layout.StaticGraphLayout;
import org.bm.blaise.specto.Plottable;
import org.bm.blaise.style.BasicStringStyle;
import org.bm.blaise.style.StringStyle;
import org.bm.firestarter.propertysheet.editor.EditorRegistration;
import org.bm.util.Delegator;

/**
 *
 * @author ae3263
 */
public class TestGraph extends javax.swing.JFrame {

    PlaneGraphAdapter pga;
    /** Flag for when el needs points updated */
    boolean updateEL = true;
    SpringLayout energyLayout;

    /** Creates new form TestPlaneVisometry */
    public TestGraph() {
        EditorRegistration.registerEditors();
        initComponents();

        // BASIC ELEMENTS

//        MyTestGraph png = new MyTestGraph();
        final PrimeNumberGraph png = GraphFactory.getPrimeNumberGraph(200, 2);
        plot.setGraph(png);
        plot.getAdapter().getNodeStyler().setStyleDelegate(new Delegator<Object, PointStyle>(){
            public PointStyle of(Object o) {
                Integer i = (Integer) o;
                return new BasicPointStyle().fill(png.getColor(i)).stroke(null).radius((int) (1 + .5 * png.getValue(i)));
            }
        });
        
        plot.getAdapter().getNodeStyler().setTipDelegate(new Delegator<Object, String>(){
            public String of(Object o) {
                Integer i = (Integer) o;
                return String.format("%d has %d prime factors: %s", i, png.getValue(i), PrimeNumberGraph.factors(i));
            }
        });
        plot.getAdapter().getNodeStyler().setLabelDelegate(new Delegator<Object, String>(){
            public String of(Object src) { return src == null ? "null" : src.toString(); }
        });
        plot.getAdapter().getNodeStyler().setLabelStyleDelegate(new Delegator<Object, StringStyle>(){
            public StringStyle of(Object o) {
                Integer i = (Integer) o;
                return new BasicStringStyle().color(png.getColor(i));
            }
        });
        

        // PANELS

        rollupPanel1.add("Visometry", new PropertySheet(plot.getVisometry()));
        rollupPanel1.add("Energy Layout", new PropertySheet(energyLayout = new SpringLayout(
                plot.getGraphManager().getLocationMap()
                )));
        for (Plottable p : plot.getPlottableArray()) {
            rollupPanel1.add(p.toString(), new PropertySheet(p));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        randomLB = new javax.swing.JButton();
        circleLB = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jLabel1 = new javax.swing.JLabel();
        energyIB = new javax.swing.JButton();
        energyAB = new javax.swing.JButton();
        energySB = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        rollupPanel1 = new org.bm.util.gui.RollupPanel();
        plot = new org.bm.blaise.specto.plane.graph.GraphComponent();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(0, 0, 0));

        jToolBar1.setRollover(true);

        randomLB.setText("Random Layout");
        randomLB.setFocusable(false);
        randomLB.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        randomLB.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        randomLB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                randomLBActionPerformed(evt);
            }
        });
        jToolBar1.add(randomLB);

        circleLB.setText("Circle Layout");
        circleLB.setFocusable(false);
        circleLB.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        circleLB.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        circleLB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                circleLBActionPerformed(evt);
            }
        });
        jToolBar1.add(circleLB);
        jToolBar1.add(jSeparator1);

        jLabel1.setText("ENERGY:");
        jToolBar1.add(jLabel1);

        energyIB.setText("iterate");
        energyIB.setFocusable(false);
        energyIB.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        energyIB.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        energyIB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                energyIBActionPerformed(evt);
            }
        });
        jToolBar1.add(energyIB);

        energyAB.setText("animate");
        energyAB.setFocusable(false);
        energyAB.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        energyAB.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        energyAB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                energyABActionPerformed(evt);
            }
        });
        jToolBar1.add(energyAB);

        energySB.setText("stop");
        energySB.setFocusable(false);
        energySB.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        energySB.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        energySB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                energySBActionPerformed(evt);
            }
        });
        jToolBar1.add(energySB);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);

        jScrollPane1.setViewportView(rollupPanel1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.EAST);
        getContentPane().add(plot, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void randomLBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_randomLBActionPerformed
        updateEL = true;
        plot.getGraphManager().applyLayout(StaticGraphLayout.RANDOM, 5.0);
    }//GEN-LAST:event_randomLBActionPerformed

    private void circleLBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_circleLBActionPerformed
        updateEL = true;
        plot.getGraphManager().applyLayout(StaticGraphLayout.CIRCLE, 5.0);
    }//GEN-LAST:event_circleLBActionPerformed

    private void energyIBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_energyIBActionPerformed
        if (energyLayout == null)
            energyLayout = new SpringLayout(plot.getGraphManager().getLocationMap());
        plot.getGraphManager().setLayoutAlgorithm(energyLayout);
        plot.getGraphManager().iterateLayout();
        updateEL = false;
    }//GEN-LAST:event_energyIBActionPerformed

    private void energyABActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_energyABActionPerformed
        if (energyLayout == null)
            energyLayout = new SpringLayout(plot.getGraphManager().getLocationMap());
        plot.getGraphManager().setLayoutAlgorithm(energyLayout);
        plot.getGraphManager().startLayoutTask(10, 2);
    }//GEN-LAST:event_energyABActionPerformed

    private void energySBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_energySBActionPerformed
        plot.getGraphManager().stopLayoutTask();
    }//GEN-LAST:event_energySBActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TestGraph().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton circleLB;
    private javax.swing.JButton energyAB;
    private javax.swing.JButton energyIB;
    private javax.swing.JButton energySB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private org.bm.blaise.specto.plane.graph.GraphComponent plot;
    private javax.swing.JButton randomLB;
    private org.bm.util.gui.RollupPanel rollupPanel1;
    // End of variables declaration//GEN-END:variables
}
